package com.qxgl.qxglt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.qxgl.qxglt.dao.IRolesDao;
import com.qxgl.qxglt.dto.RolesDTO;
import com.qxgl.qxglt.model.Roles;
import com.qxgl.qxglt.service.IRolesService;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@ApiOperation("角色列表")
@RestController
@RequestMapping("/roles")
public class RolesController {
    @Resource
    private IRolesDao rolesDao;
    @Resource
    private IRolesService rolesService;

    @ApiOperation(value = "分页查询")
    @PostMapping(value = "findPage")
    public Map<String, Object> findAll(RolesDTO dto){
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Roles> page = rolesService.findPage(dto);
        map.put("code", 0);
        map.put("msg", "查询出功");
        map.put("count",page.getTotalElements());
        map.put("data", page.getContent());
        return map;
    }
    @ApiOperation(value = "查询所有")
    @PostMapping(value = "findAll")
    public Map<String, Object> findAll(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Roles> list = rolesDao.findAll();
        map.put("code", 0);
        map.put("msg", "查询出功");
        map.put("count",list.size());
        map.put("data", list);
        return map;
    }

    @ApiOperation(value = "更新用户信息", notes = "添加或修改")
    @PostMapping("/addOrEdit")
    public Roles saveMember(@RequestBody Roles r) {
        return rolesDao.save(r);
    }

    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "编号", required = true, paramType = "path")
    @DeleteMapping(value = "/{id}")
    public int del(@PathVariable("id") String id) {
        return rolesDao.delById(id);

    }
    @ApiOperation(value = "给角色设置权限")
    @PostMapping("/setModules")
    public void setModules( String role_id,@RequestParam List<String> roleIdList){
        rolesService.setModules(role_id, roleIdList);
    }


}